package com.isfto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isfto.code.PageResult;
import com.isfto.dao.UserDao;
import com.isfto.entity.User;
import com.isfto.service.UserService;
import com.isfto.until.Md5Util;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    JavaMailSender javaMailSender;

    @Value( "${spring.mail.username}" )
    String from;




    @Autowired
    private UserDao userDao;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        String pass= Md5Util.getMD5String(password);
        queryWrapper.eq( "username",username ).eq( "password",pass );
        User user = userDao.selectOne( queryWrapper );
        if (user.getIslock() ==0){
            return  null;
        }
        return user;
    }

    @Override
    public Boolean adduser(User user) {
        if (user.getUsername()==null||user.getPassword()==null){
            return false;
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper=queryWrapper.eq( User::getUsername,user.getUsername() );
        User user1 = userDao.selectOne( queryWrapper );
        if (user1!=null){
            return false;
        }
        user.setRole( "common" );
        user.setIslock( 1 );
        String pas= Md5Util.getMD5String(user.getPassword());
        user.setPassword(pas);
        userDao.insert( user );
        sendemail(user.getEmail());

        return true;

    }


    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageResult getUsers(int pageNo, int pageSize) {
        Page<User> page = new Page<>(pageNo, pageSize);
        // 示例查询条件，可以根据实际需求修改
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "role","common" );
        userDao.selectPage(page, queryWrapper);
        List<User> userList = page.getRecords();
        long total = userList.size();
        System.out.println(total);

        // 构建返回结果
        PageResult pageResult = new PageResult();
        pageResult.setRecords(userList);
        pageResult.setTotal(total);

        return pageResult;
    }

    @Override
    public Boolean delById(Integer id) {
        int i = userDao.deleteById( id );
        if (i>=1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 注册成功发送邮件
     */
    private void sendemail(String email) {
        MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                String text = "<html><head><meta charest=\"utf-8\"</head><body><h1>天津滨海机场</h1><p>恭喜您注册成功</p></body>";
                MimeMessageHelper helper = new MimeMessageHelper( mimeMessage,true );
                helper.setFrom( from );
                helper.setTo(email);

                helper.setSubject( "注册信息" );
                helper.setText( text,true );

            }
        };

        javaMailSender.send( messagePreparator );

    }

}
